package com.csdj.rest.controller;

import com.csdj.beans.entity.Config;
import com.csdj.common.WebResult;
import com.csdj.common.constant.WebResultConstant;
import com.csdj.rest.service.ConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class ConfigController {

    @Resource
    private ConfigService configService;

    @PutMapping("/config")
    public WebResult  add(Config config){
        if(configService.addConfig(config)!=null){
            return WebResult.success("");
        }
        return WebResult.fail(WebResultConstant.FAIL);
    }

    @GetMapping("/config")
    public WebResult  get(String type,String key){
        final Config config = configService.getConfig(type, key);
        return new WebResult(configService.getConfig(type,key));
    }


    @PostMapping("/update")
    public WebResult update(Config config){
        return new WebResult(configService.update(config));
    }

    @GetMapping("/list")
    public WebResult list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                          @RequestParam(value = "rows",defaultValue = "12")Integer rows){
        return WebResult.success(configService.getByPage(page,rows))
                .putData("total",configService.getCount());
    }

}
